<template>
  <div>
    <el-row style="margin: 100px 0 0 0;">
      <el-col :span="7">&nbsp;</el-col>
      <el-col :span="8">
        <h1 style="text-align: left; margin: 20px 0 20px 220px;color: white">拾文博客登录</h1>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px  "  class="demo-ruleForm">
          <el-form-item label="用户名" prop="username" >
            <el-input v-model="ruleForm.username" style="width: 500px ;height: 30px ;margin-bottom: 40px"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" style="width: 500px ;height: 30px;margin-bottom: 40px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="height: 50px ;width: 100px;margin-right: 50px"  type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button style="height: 50px ;width: 100px" @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          let url = 'http://localhost:9090/login';
          console.log('url >>> ' + url);
          let data = {
            'username': this.ruleForm.username,
            'password': this.ruleForm.password
          };
          console.log('data >>> ');
          console.log(data);
          this.axios.post(url, data).then((response) => {
            console.log(response.data);
            if (response.data == 1) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
            } else if (response.data == 2) {
              this.$message.error('登录失败，用户名不存在！');
            } else {
              this.$message.error('登录失败，密码错误！');
            }
          }).catch(function(error) {
            console.log('响应结果为失败！');
          })
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style>

body{
  align-content: center;
background-image: url("../assets/back.jpg");
}
</style>